package com.example.minimalist_bookkeeping.utils;

import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.view.inputmethod.InputMethodManager;

import com.example.minimalist_bookkeeping.R;

public class KeyBoardUtils {
    private final KeyboardView keyboardView;
    private final EditText editText;

    public interface OnEnsureListener {
        void onEnsure();
    }

    private OnEnsureListener onEnsureListener;

    public KeyBoardUtils(KeyboardView keyboardView, EditText editText) {
        this.keyboardView = keyboardView;
        this.editText = editText;
        this.editText.setInputType(android.text.InputType.TYPE_CLASS_NUMBER); // 防止系统键盘弹出

        // 替代弃用的构造函数
        Keyboard keyboard = new Keyboard(editText.getContext(), R.xml.key);
        keyboardView.setKeyboard(keyboard);
        keyboardView.setEnabled(true);
        keyboardView.setPreviewEnabled(false);
        keyboardView.setOnKeyboardActionListener(listener);
    }

    private KeyboardView.OnKeyboardActionListener listener = new KeyboardView.OnKeyboardActionListener() {
        @Override
        public void onPress(int primaryCode) {}

        @Override
        public void onRelease(int primaryCode) {}

        @Override
        public void onKey(int primaryCode, int[] keyCodes) {
            Editable editable = editText.getText();
            int start = editText.getSelectionStart();
            switch (primaryCode) {
                case Keyboard.KEYCODE_DELETE:
                    if (editable != null && editable.length() > 0 && start > 0) {
                        editable.delete(start - 1, start);
                    }
                    break;
                case Keyboard.KEYCODE_CANCEL:
                    editable.clear();
                    break;
                case Keyboard.KEYCODE_DONE:
                    if (onEnsureListener != null) {
                        onEnsureListener.onEnsure(); // 回调确定事件
                    }
                    hideSystemKeyboard(); // 隐藏系统键盘
                    break;
                default:
                    if (primaryCode >= 0) {
                        editable.insert(start, Character.toString((char) primaryCode));
                    }
                    break;
            }
        }

        private void hideSystemKeyboard() {
            InputMethodManager imm = (InputMethodManager) editText.getContext()
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
            }
        }

        @Override
        public void onText(CharSequence text) {}

        @Override
        public void swipeLeft() {}

        @Override
        public void swipeRight() {}

        @Override
        public void swipeDown() {}

        @Override
        public void swipeUp() {}
    };

    public void setOnEnsureListener(OnEnsureListener onEnsureListener) {
        this.onEnsureListener = onEnsureListener;
    }

    public void showKeyboard() {
        keyboardView.setVisibility(View.VISIBLE);
    }

    public void hideKeyboard() {
        keyboardView.setVisibility(View.GONE);
    }
}



